clc;
clear all;
%close all;
global c B K T Tc fs f0 lambda d
c=3.0e8;  
B=768e6;       %调频带宽
K=29.982e12;       %调频斜率
T=B/K;         %调频周期
Tc=160e-6;     %chirp总周期
fs=1e7;       %采样率
f0=77e9;       %初始频率
lambda=c/f0;   %雷达信号波长
d=lambda/2;    %天线阵列间距

N = 256;       %距离向FFT点数
M = 128;       %多普勒向FFT点数
Q = 180;       %角度FFT
%读取数据，能显示每帧数据
%%% This script is used to read the binary file produced by the DCA1000
%%% and Mmwave Studio
%%% Command to run in Matlab GUI -
%readDCA1000('<ADC capture bin file>') function [retVal] = readDCA1000(fileName)
%% global variables
% change based on sensor config
numADCSamples = 256; % number of ADC samples per chirp
numADCBits = 16; % number of ADC bits per sample
numRX = 4; % number of receivers
numLanes = 2; % do not change. number of lanes is always 2
isReal = 0; % set to 1 if real only data, 0 if complex data0
n_chirps=128;%每一帧中chirps数，no of chirp loops 
% frame=50;%第多少帧
%% read file
% read .bin file
fileName='D:\20241022\5.bin';
fid = fopen(fileName,'r');
adcData = fread(fid, 'int16');
% if 12 or 14 bits ADC per sample compensate for sign extension
if numADCBits ~= 16
    l_max = 2^(numADCBits-1)-1;
    adcData(adcData > l_max) = adcData(adcData > l_max) - 2^numADCBits;
end
fclose(fid);
fileSize = size(adcData, 1);
% real data reshape, filesize = numADCSamples*numChirps
if isReal
    numChirps = fileSize/numADCSamples/numRX;
    LVDS = zeros(1, fileSize);
    %create column for each chirp
    LVDS = reshape(adcData, numADCSamples*numRX, numChirps);
    %each row is data from one chirp
    LVDS = LVDS.';
else
    % for complex data
    % filesize = 2 * numADCSamples*numChirps
    numChirps = fileSize/2/numADCSamples/numRX;%总chirps数
    numframe=numChirps/n_chirps;%总帧数 no of frames，总chirps数除以每帧chirps数。
    LVDS = zeros(1, fileSize/2);
    %combine real and imaginary part into complex data
    %read in file: 2I is followed by 2Q
    counter = 1;
    for i=1:4:fileSize-1
        LVDS(1,counter) = adcData(i) + sqrt(-1)*adcData(i+2); 
        LVDS(1,counter+1) = adcData(i+1)+sqrt(-1)*adcData(i+3); 
        counter = counter + 2;
    end
        % create column for each chirp
        LVDS = reshape(LVDS, numADCSamples*numRX, numChirps);
        %each row is data from one chirp
        LVDS = LVDS.';
end
%organize data per RX
adcData = zeros(numRX,numChirps*numADCSamples);
for row = 1:numRX
    for i = 1: numChirps
        adcData(row, (i-1)*numADCSamples+1:i*numADCSamples) = LVDS(i, (row-1)*numADCSamples+1:row*numADCSamples);
    end
end
% return receiver data
%adcData;接收的总数据retVal = adcData(:,(frame-1)*numADCSamples*n_chirps+1:frame*numADCSamples*n_chirps);%读取每帧数据

data = reshape(adcData,numADCSamples,n_chirps,numRX,numframe);
data1 = data(:,:,1,1);
data2 = data(:,:,2,1);
data3 = data(:,:,3,1);
data4 = data(:,:,4,1);
% [range,speed,angle]  = threedfft(data_radar, N, M, Q, numADCSamples,numChirps,numRX);
for f= 1:3%numframe
    [range(:,:,:,f),speed(:,:,:,f),angle(:,:,:,f)]  = threedfft(data(:,:,:,f), N, M, Q, numADCSamples,n_chirps,numRX);
end
% [range(:,:,:,1),speed(:,:,:,1),angle(:,:,:,1)]  = threedfft(data(:,:,:,1), N, M, Q, numADCSamples,n_chirps,numRX);

plotRangeSpeed(speed(:,:,1,:), N, M, fs, 1,3); 

% for i = 1:5
% %figure;
% speed1 = reshape(speed(:,:,i),N,M);   
% speed_Temp = speed1';
% [X,Y]=meshgrid((0:N-1)*fs*c/N/2/K,(-M/2:M/2-1)*lambda/Tc/M/2);
% mesh(X,Y,(abs(speed_Temp))); 
% xlabel('距离(m)');
% ylabel('速度(m/s)');
% zlabel('信号幅值');
% title(sprintf('2维FFT视图, 第 %d 个frame', i));
% xlim([0 (N-1)*fs*c/N/2/K]);
% ylim([(-M/2)*lambda/Tc/M/2 (M/2-1)*lambda/Tc/M/2]);
% pause(1);
% %close;
% end

